package httpconnect;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by zy on 2018/10/17.
 */
public class ChapterBean implements CallBack {
    @Override
    public void responseData(String data,CallBack callBack) {
        pares(data);
    }

    public void pares(String data) {
        Document document = Jsoup.parse(data);
        Element content = document.getElementById("content");
        Elements title = document.getElementsByTag("h1");
        String strCon = content.text();
        strCon = strCon.replace(" ", "\n");
        for (Element element : title) {
            if (element.hasParent() && element.parent().hasClass("bookname")) {
                String strTitle = element.text();
                try {
                    FileWriter fileWriter = new FileWriter(new File(Const.book), true);
                    fileWriter.write("\n\n");
                    fileWriter.write("\t" + strTitle);
                    fileWriter.write("\n\n");
                    fileWriter.write(strCon);
                    fileWriter.write("\n\n");
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }
}
